<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改资源</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form layui-form-pane" action="" name="frmMain">
    <input type="hidden" name="id" >
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item ">
                <label class="layui-form-label">所属目录</label>
                <div class="layui-input-block">
                    <ul id="dir" class="dtree" data-id="-1"></ul>
                </div>
            </div>
            <div class="layui-form-item ">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="resName" lay-verify="required" autocomplete="off" placeholder="请输入名称"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <select name="res_type" lay-verify="required" lay-filter="res_type" disabled class="layui-disabled">
                        <option value="">请选择类型</option>
                        <option value="1">目录</option>
                        <option value="2">链接</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label">链接</label>
                <div class="layui-input-block">
                    <input type="text" name="link" autocomplete="off" placeholder="请输入链接"
                           class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</form>
<script src="../../../config/apiUrl.js"></script>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'http', 'popup', 'common', 'toast', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let http = layui.http;
        let popup = layui.popup;
        let common = layui.common;
        let toast = layui.toast;
        let dtree = layui.dtree;
        let res_id = common.getUrlParams("id");
        $("body").on("click", function(event){
            $("div[dtree-id][dtree-select]").removeClass("layui-form-selected");
            $("div[dtree-id][dtree-card]").removeClass("dtree-select-show layui-anim layui-anim-upbit");
        });
        if (res_id != null) {
            let loading = layer.load();
            //查询单条数据
            http.ajax({
                url: apiUrlConfig.res.GetById + res_id,
                dataType: 'json',
                type: 'get',
                contentType: "application/json",
                success: function (result) {
                    layer.close(loading);
                    if (result.status == 200) {
                        frmMain.id.value = result.data.id;
                        frmMain.resName.value = result.data.resName;
                        frmMain.res_type.value = result.data.res_type;
                        frmMain.link.value = (result.data.link == null ? "" : result.data.link);
                        if (frmMain.res_type.value == 2) {
                            $("[name='link']").parent().parent().show();
                        }
                        dtree.render({
                            elem: "#dir",
                            initLevel: "1",
                            method: 'post',
                            url: apiUrlConfig.res.ListDir,
                            select: true,
                            dataFormat: "list",
                            skin: "laySimple",  // laySimple主题风格
                            response:{
                                statusName: "status", //返回标识（必填）
                                statusCode: 200, //返回码（必填）
                                message: "msg", //返回信息（必填）
                                rootName: "data", //根节点名称（必填）
                                treeId: "id", //节点ID（必填）
                                parentId: "pid", //父节点ID（必填）
                                title: "resName", //节点名称（必填）
                            },
                            menubar: true, //开启菜单栏
                            done:function (res, $ul, first){
                                if(first){
                                    let v = result.data.pid+"";
                                    dtree.selectVal("dir", v);
                                    dtree.dataInit("dir", v);
                                }
                            }
                        });
                        form.render();
                    } else {
                        toast.error({title: false, message: result.msg})
                    }
                },
                error: function (res) {
                    layer.close(loading);
                }
            })

        }

        function closeWin() {
            let layer_index = parent.layer.getFrameIndex(window.name);
            if (layer_index > 0) {
                parent.layer.close(layer_index); //关闭当前页
            }
        }

        $(document).keyup(function (event) {
            if (event.keyCode == 27) {
                closeWin();
            }
        });
        form.on('select(res_type)', function (data) {
            var elem = data.elem; // 获得 select 原始 DOM 对象
            var value = data.value; // 获得被选中的值
            if (value == 1) {
                $("[name='link']").parent().parent().hide();
            } else if (value == 2) {
                $("[name='link']").parent().parent().show();
            } else {
                $("[name='link']").parent().parent().hide();
            }
        });
        form.on('submit(user-save)', function (data) {
            let loading = layer.load();
            if(data.field.dir_select_nodeId != ''){
                data.field.pid = data.field.dir_select_nodeId;
            }
            http.ajax({
                url: apiUrlConfig.res.Update,
                dataType: 'json',
                type: 'post',
                data: JSON.stringify(data.field),
                success: function (result) {
                    layer.close(loading);
                    if (result.status == 200) {
                        parent.layui.toast.success({title: false, message: '保存成功'})
                        const bc = new BroadcastChannel('resListReload');
                        bc.postMessage("resListReload");
                        closeWin();
                    } else {
                        if (result.status == 10001) {
                            //字段提示
                            let errorMsg = JSON.parse(result.msg);
                            for (let key in errorMsg) {
                                layer.tips(errorMsg[key], $("[name='" + key + "']").parent(), {
                                    tips: [3, '#f56c6c'], time: 5000, tipsMore: true
                                });
                            }
                        } else {
                            toast.error({title: false, message: result.msg})
                        }
                    }
                },
                error: function (res) {
                    layer.close(loading);
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>
